package cn.gailvlun.gll.presentation.emotion.widget;

import android.content.Context;
import android.databinding.BindingAdapter;
import android.text.Editable;
import android.util.AttributeSet;

import com.vanniktech.emoji.EmojiEditText;

import cn.gailvlun.gll.util.IsEmpty;



public class CustomTextView extends EmojiEditText {
    public CustomTextView(Context context) {
        super(context);
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @BindingAdapter("setText")
    public static void setText(CustomTextView ctv, String content) {
        if (ctv != null) {
            ctv.setText(content);
        }
    }

    public void setText(String content) {



    }

    @BindingAdapter("appendTextWithQuotes")
    public static void appendTextWithQuotes(CustomTextView ctv, String content) {
        if (ctv != null) {
            if (!IsEmpty.string(content)) {
                ctv.appendTextWithQuotes(content);
            }
        }
    }

    public static final char quote = '\"';

    public void appendTextWithQuotes(String text) {
        int index = getSelectionStart();
        Editable edit = getEditableText();
        if ( (index < 0 || index >= edit.length())) {
            edit.append(quote);
            edit.append(text);
            edit.append(quote);
        }  else {
            edit.insert(index, text);
        }
        requestFocus();
    }

    public void appendText(String text) {
        int index = getSelectionStart();
        Editable edit = getEditableText();
        if (index < 0 || index >= edit.length()) {
            edit.append(text);
        } else {
            edit.insert(index, text);
        }
    }
}
